package com.daocao.ds.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.ds.R;
import com.daocao.ds.api.Api;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.util.UI;
import com.daocao.ds.util.Util;
import com.daocao.ds.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SettingActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_change_phone)
    TextView txtChangePhone;
    @BindView(R.id.btn_change_phone)
    LinearLayout btnChangePhone;
    @BindView(R.id.txt_login_pass)
    TextView txtLoginPass;
    @BindView(R.id.btn_login_pass)
    LinearLayout btnLoginPass;
    @BindView(R.id.txt_gesture_pass)
    TextView txtGesturePass;
    @BindView(R.id.btn_gesture_pass)
    LinearLayout btnGesturePass;
    @BindView(R.id.txt_help_center)
    TextView txtHelpCenter;
    @BindView(R.id.btn_help_center)
    LinearLayout btnHelpCenter;
    @BindView(R.id.btn_logout)
    TextView btnLogout;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("设置");

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!UserInfoGlobal.checkLogin()) {
            btnLogout.setText("登录");
        } else {
            btnLogout.setText("退出登录");
        }
        if (UserInfoGlobal.getIsOpenGuesture()) {
            txtGesturePass.setText("已开启");
        } else {
            txtGesturePass.setText("未开启");
        }
    }

    public void logout() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        Api.api_service.getLogoutInfo().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.logout();
                    btnLogout.setText("登录");
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_change_phone, R.id.btn_login_pass, R.id.btn_gesture_pass, R.id.btn_help_center, R.id.btn_logout})
    public void onViewClicked(View view) {
        if(UI.isFastClick()){
            return;
        }
        switch (view.getId()) {

            case R.id.btn_login_pass:
                if (UserInfoGlobal.getIsSetPayPass()) {
                    Util.startActivity(this, HasSetTransactionActivity.class);
                } else {
                    Util.startActivity(this, TransationSetStepOneActivity.class);
                }

                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_change_phone:
                Util.startActivity(this, SnatchingModeActivity.class);
                break;
            case R.id.btn_gesture_pass:
//                Util.startActivity(this, GestureSettingActivity.class);
                break;
            case R.id.btn_help_center:
//                rxFingerPrinter.begin().subscribe(observer);//RxfingerPrinter会自动在onPause()时暂停指纹监听，onResume()时恢复指纹监听)
                Util.startActivity(this, WechatChatActivity.class);
                break;
            case R.id.btn_logout:
                if (UserInfoGlobal.checkLogin()) {
                    logout();

                } else {
                    Util.startActivity(this, LoginByPassActivity.class);
                    finish();
                }
                break;
        }
    }
}
